package com.legal.app.common;

import com.legal.common.exception.GlobalException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/*
 * @Description 全局异常处理
 * @Author Ale
 * @Date 2025/6/14 17:06
 */
@RestControllerAdvice(basePackages = "com.legal.app")
@Component("appGlobalExceptionHandler")
public class GlobalExceptionHandler {

    // 初始化日志记录器
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    // 处理全局异常
    @ExceptionHandler(GlobalException.class)
    public AjaxResult handleGlobalException(GlobalException e) {
        return AjaxResult.error(e.getMessage());
    }


    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    public AjaxResult handleAccessDeniedException() {
        return AjaxResult.error(403, "权限不足");
    }
    // 处理其他系统异常
    @ExceptionHandler(Exception.class)
    public AjaxResult handleException(Exception e) {
        log.error("系统异常：", e);
        return AjaxResult.error("系统内部错误，请联系管理员");
    }
}
